package com.yyx.Imitate;

import com.yyx.mapper.TestMapperFu;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class MyFactoryBean implements FactoryBean, InvocationHandler {
    Class clazz;
    public MyFactoryBean(Class clazz){
        this.clazz=clazz;
    }
    @Override
    public TestMapperFu getObject() throws Exception {
        Class[] classes = new Class[]{clazz};
        //mapper接口生成代理对象，放入factoryBean，以便spring bean初始化进行
        TestMapperFu proxy = (TestMapperFu) Proxy.newProxyInstance(this.getClass().getClassLoader(),classes,this);
        return proxy;
    }

    @Override
    public Class<?> getObjectType() {
        return clazz;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("proxy");

        Method method1 = proxy.getClass().getInterfaces()[0].getMethod(method.getName());
        Select select = method1.getDeclaredAnnotation(Select.class);
//        method1.invoke(clazz.newInstance());
        //        TestMapper testMapper= (TestMapper) proxy;
        return null;
    }


}
